<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>web 剪贴板</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <div>
        <!-- 文本域 -->
        <div style="text-align: center;">
            <textarea id="code" cols="30" rows="10" style="width: 90vw; height: 80vh;" autofocus></textarea>
        </div>
        <!-- 按钮 -->
        <div style="text-align: center;">
            <button onclick="send()">提交</button><br>
            <span id='status'></span>
        </div>
    </div>

    <script>
        // URL
        let url = window.location.protocol + '//' + window.location.host;
        let url_paste = url + '/paste';

        // DOM
        let codeElem = document.getElementById('code');
        let statusElem = document.getElementById('status');

        // 发送文本
        function send() {
            let code = codeElem.value;
            statusElem.innerHTML = 'fail';
            loadXMLDocPost(url_paste, code, successHandler);
        }

        // 成功回调
        function successHandler() {
            statusElem.innerHTML = 'success';
        }

        // 封装的AJAX POST代码
        function loadXMLDocPost(url, data, cfunc) {
            var xmlhttp;
            if (window.XMLHttpRequest) {
                //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
                xmlhttp = new XMLHttpRequest();
            }
            else {
                // IE6, IE5 浏览器执行代码
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    cfunc(xmlhttp);
                }
            };
            xmlhttp.open("POST", url, true);
            xmlhttp.send(data);
        }
    </script>
</body>

</html>